package com.example.finalwork2.component;

import com.example.finalwork2.dto.WarnRequestItem;
import com.example.finalwork2.entity.VehicleSignal;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import java.util.Map;


//这是适配器，用来适应我的判断方法
@Component
public class VehicleSignalAdapter {

    private final ObjectMapper objectMapper = new ObjectMapper();

    public VehicleSignal adapt(WarnRequestItem requestItem, String batteryType) throws Exception {
        Map<String, Double> signalMap = objectMapper.readValue(requestItem.getSignal(), new TypeReference<Map<String, Double>>() {});


        VehicleSignal signal = new VehicleSignal();
        signal.setVehicleId(String.valueOf(requestItem.getCarId()));
        //电池类型
        signal.setBatteryType(batteryType);

        //电压电流
        signal.setMx(signalMap.get("Mx"));
        signal.setMi(signalMap.get("Mi"));
        signal.setIx(signalMap.get("Ix"));
        signal.setIi(signalMap.get("Ii"));

        return signal;
    }
}
